package com.todo.biz.application.mq.sender;

import com.alibaba.fastjson.JSONObject;
import com.todo.biz.application.base.conf.BizBaseRabbitConfig;
import com.todo.biz.application.mq.entity.BizCountMQEntity;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * @author azhebuxing
 * @date 2024/12/11 02:36
 * @description
 */
@Component
public class BizCounterMqSender {

    @Resource
    private AmqpTemplate rabbitTemplate;

    public void sendContentCounter(BizCountMQEntity entity, Integer delayTime) {
        if (delayTime == null || delayTime == 0) {
            rabbitTemplate.convertAndSend(BizBaseRabbitConfig.EXCHANGE_BIZ_BASE, BizBaseRabbitConfig.QUEUE_CONTENT_COUNT, JSONObject.toJSONString(entity), message -> {
                message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
                message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                return message;
            });
        } else {
            rabbitTemplate.convertAndSend(BizBaseRabbitConfig.EXCHANGE_BIZ_BASE, BizBaseRabbitConfig.QUEUE_CONTENT_COUNT, JSONObject.toJSONString(entity), message -> {
                message.getMessageProperties().setDelay(delayTime);
                message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
                message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                return message;
            });
        }
    }

    public void sendSightCounter(BizCountMQEntity entity, Integer delayTime) {
        if (delayTime == null || delayTime == 0) {
            rabbitTemplate.convertAndSend(BizBaseRabbitConfig.EXCHANGE_BIZ_BASE, BizBaseRabbitConfig.QUEUE_SIGHT_COUNT, JSONObject.toJSONString(entity), message -> {
                message.getMessageProperties().setDelay(delayTime);
                message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
                message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                return message;
            });
        } else {
            rabbitTemplate.convertAndSend(BizBaseRabbitConfig.EXCHANGE_BIZ_BASE, BizBaseRabbitConfig.QUEUE_SIGHT_COUNT, JSONObject.toJSONString(entity), message -> {
                message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
                message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                return message;
            });
        }
    }

    public void sendPlatformStrategyCounter(BizCountMQEntity entity, Integer delayTime) {
        if (delayTime == null || delayTime == 0) {
            rabbitTemplate.convertAndSend(BizBaseRabbitConfig.EXCHANGE_BIZ_BASE, BizBaseRabbitConfig.QUEUE_PLATFORM_STRATEGY_COUNT, JSONObject.toJSONString(entity), message -> {
                message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
                message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                return message;
            });
        } else {
            rabbitTemplate.convertAndSend(BizBaseRabbitConfig.EXCHANGE_BIZ_BASE, BizBaseRabbitConfig.QUEUE_PLATFORM_STRATEGY_COUNT, JSONObject.toJSONString(entity), message -> {
                message.getMessageProperties().setDelay(delayTime);
                message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
                message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                return message;
            });
        }
    }

    public void sendUserStrategyCounter(BizCountMQEntity entity, Integer delayTime) {
        if (delayTime == null || delayTime == 0) {
            rabbitTemplate.convertAndSend(BizBaseRabbitConfig.EXCHANGE_BIZ_BASE, BizBaseRabbitConfig.QUEUE_USER_STRATEGY_COUNT, JSONObject.toJSONString(entity), message -> {
                message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
                message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                return message;
            });
        } else {
            rabbitTemplate.convertAndSend(BizBaseRabbitConfig.EXCHANGE_BIZ_BASE, BizBaseRabbitConfig.QUEUE_USER_STRATEGY_COUNT, JSONObject.toJSONString(entity), message -> {
                message.getMessageProperties().setDelay(delayTime);
                message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
                message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                return message;
            });
        }
    }

}
